<?php
class group extends parent_class
{
	public $id=0;
	public $title="";
	public $description="";
	public $rate=0;
	public $teacher=0;
	
	public function __construct($id=0)
	{
		if ($id>0)
		{
			return $this->get($id);
		}
	}
	
	public function get($id=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		$query="SELECT * FROM `groups` WHERE `id`='".intval($id)."' LIMIT 1";
		if ($result=db::$db->query($query))
		{
			if ($result->num_rows==1)
			{
				$row=$result->fetch_object();
				$this->id=$row->id;
				$this->title=$row->title;
				$this->description=$row->description;
				$this->rate=$row->rate;
				$this->teacher=$row->teacher;
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			$this->error(2);
		}
	}

	public function remove($id=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		if (!($this->exist()))
		{
			return false;
		}
		
		$query="DELETE FROM `groups` WHERE `id`='".intval($id)."'LIMIT 1";
		
		if (db::$db->query($query))
		{
			return true;
		}
		else
		{
			$this->error(2);
		}
	}
	public function update($id=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		
		if (!$this->exist())
		{
			return false;
		}
		
		if (trim($this->title)=="")
		{
			return false;
		}
		$tmp=new user($this->group);
		$query="UPDATE `groups` SET `title`='$this->title', 
									`description`='$this->description', `rate`='$this->rate', 
									`teacher`='$this->teacher' WHERE `id`='".$id."'";
		if ($result=db::$db->query($query))
		{
			return true;
		}
		else
		{
			$this->error(2);
		}
	}
	
	public function exist($id=0)
	{
		if ($id<1)
		{
			if ($this->id>0)
			{
				$id=$this->id;
			}
			else
			{
				return false;
			}
		}
		$query="SELECT `id` FROM `groups` WHERE `id`='$id' LIMIT 1";
		if ($result=db::$db->query($query))
		{
			if ($result->num_rows==1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
			$this->error(2);
		}
	}
	
	public function add()
	{
		if (trim($this->title)=="")
		{
			return false;
		}
		
		$query="INSERT INTO `groups` VALUES 
				(NULL,'$this->title','$this->description','$this->rate','$this->teacher')";
		if (db::$db->query($query))
		{
			return true;
		}
		else
		{
			$this->error(2);
		}
	}
	public function returnit($str="")
	{
		$parse=new parser;
		$parse->additem("%id%",$this->id);
		$parse->additem("%title%",$this->title);
		$parse->additem("%description%",$this->description);
		$parse->additem("%rate%",$this->rate);
		$teacher=new user($this->teacher);
		$parse->additem("%teacher_title%",$teacher->lastname." ".strtoupper(substr($teacher->firstname,0,1)).". ".strtoupper(substr($teacher->patronymie,0,1)).".");
		$parse->additem("%teacher%",$this->teacher);
		return $parse->parse($str);
	}
}
?>